

var isValidBST = function(root: TreeNode<number>): boolean {
    function loop(x: TreeNode<number>, min: number, max: number): boolean {
        let left = (!x.left || (x.left.val < x.val && x.left.val > min && loop(x.left, min, x.val)))
        let right = (!x.right || (x.right.val > x.val && x.right.val < max && loop(x.right, x.val, max)))
        return left && right
    }
    return root === null || loop(root, Number.MIN_SAFE_INTEGER, Number.MAX_VALUE)
}

var input98: TreeNode<number> = null

input98 = {
    val: 3, 
    left: { 
        val: 1, 
        left: { val: 0 }, 
        right: { val: 2 }, 
    }, 
    right: { 
        val: 5, 
        left: { val: 4 }, 
        right: { val: 6 }, 
    }
}
console.assert( isValidBST( input98))


console.assert( isValidBST( null))

input98 = {
    val: 2, left: { val: 1, left: null, right: null }, right: { val: 3, left: null, right: null }
}
console.assert( isValidBST( input98))

input98 = {
    val: 1, left: { val: 2, left: null, right: null }, right: { val: 3, left: null, right: null }
}
console.assert( isValidBST( input98) == false)

input98 = {
    val: 10, 
    left: { val: 5, left: null, right: null }, 
    right: { 
        val: 15, 
        left: { val: 6, left: null, right: null }, 
        right: { val: 20, left: null, right: null }, 
    }
}
console.assert( isValidBST( input98) == false)

console.assert( isValidBST( { val: 0, left: { val: -1, left: null, right: null }, right: null }))

console.log('The END')

